<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/11/28
 * Time: 10:20
 */
namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\Session;
use app\admin\model\Banner as BannerModel;
class Comment extends Common
{
    /*
    * 敏感词的管理
    */
    public function mingan(){

        //获取数据库的信息用于展示
        if (Request::instance()->isAjax()){
            $pagedata = Request::instance()->get();
            $pagesize = $pagedata['limit'];
            $page  = (int)$pagedata['page'];
            if ($page == 1) {
                $startrow = 0;
            }else{
                $startrow = ($page-1)*$pagesize;
            }
            $where['is_del']=array('eq',0);
            if (isset($pagedata['user_tel']) && $pagedata['user_tel'] != ''){
                $where['content'] = array('like','%'.$pagedata['user_tel'].'%');
            }
            $count = Db::name('mingan')->where($where)->count();
            $data = Db::name('mingan')->field("id,content,create_time")
                ->limit($startrow,$pagesize)
                ->where($where)
                ->order('id','desc')
                ->select();
            return json(array('code'=>0,'msg'=>'请求成功','count'=>$count,'data'=>$data));

        }else{
            return $this->fetch();
        }

    }
    /*
     * 添加敏感字
     */
    public function add(){
        if(Request::instance()->isAjax()){
            $post = Request::instance()->post();
            $post = $post['data'];
            //检查用户名是否存在
            $check = Db::name('mingan')->where("content",$post['content'])->find();
            if ($check){
                return ['code'=>'敏感词已经存在'];
                exit;
            }
            $data = [
                'content'=>$post['content'],
                'create_time'=>date("Y-m-d H:i:s"),

            ];

            $res = Db::name('mingan')->insert($data);

            if ($res){
                return ['code'=>0];
            }else{
                return ['code'=>'添加失败'];
            }
        }

    }

    /*
     * 删除敏感字
     */
    public function delMingan()
    {
        if (Request::instance()->isAjax()){
            $post = Request::instance()->post();
            $res = Db::name('mingan')->where('id',$post['id'])->delete();
            if($res !== false){
                return "1";
            }else{
                return "2";
            }
        }
    }

    /*
     * 修改敏感字
     */
    public function editMingan()
    {
        if (Request::instance()->isAjax()){
            $post = Request::instance()->post();
            $saveData = array(
                'content'=>$post['data']['content'],

            );

            $where['content'] = array('eq',$post['data']['content']);
            $where['id'] = array('neq',$post['data']['id']);

            //检查用户名是否存在
            $check = Db::name('mingan')->where($where)->find();
            if ($check){
                return ['code'=>'敏感字已存在'];
                exit;
            }

            $res = Db::name('mingan')->where('id',$post['data']['id'])->update($saveData);

            if ($res!==false){
                return ['code'=>0];
            }else{
                return ['code'=>1];
            }
        }
    }
}
